package org.bob.web.controller;

import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import org.bob.web.model.Document;
import org.bob.web.model.SearchRes;
import reactor.core.publisher.Mono;

import java.util.function.Function;
import java.util.stream.Collectors;

public class BaseController {

    private static Function<Hit<Object>, Document> HIT_MAPPER = hit -> Document.builder().index(hit.index()).id(hit.id()).score(hit.score()).source(hit.source()).build();

    protected  Mono<SearchRes>  toMono(SearchResponse<Object> searchResponse){
        SearchRes searchRes = new SearchRes();
        searchRes.setData(searchResponse.hits().hits().stream().map(HIT_MAPPER).collect(Collectors.toList()));
        searchRes.setSize(searchRes.getData().size());
        return Mono.just(searchRes);
    }


}
